/**
 * @description
 */


package com.yjxxt.dao;

import com.yjxxt.empWithDept.empWithDept;
import com.yjxxt.vo.Dept;
import com.yjxxt.vo.Emp;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;


import java.io.IOException;
import java.io.InputStream;
import java.util.List;


import static org.junit.jupiter.api.Assertions.assertEquals;


public class DeptDaoTest {
    static SqlSessionFactory sqlSessionFactory =null;
    SqlSession session = null;
    @BeforeAll
    public static void setUp() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }
    @BeforeEach
    public void initSqlSession() {
       session = sqlSessionFactory.openSession(false);
    }

    @AfterEach
    public void closeSqlSession() {
        if(session != null) {
            session.close();
        }
    }


//    @Test
//    public void testFindAllDeptNamesAndManagers() {
//        IDeptDao deptDao = session.getMapper(IDeptDao.class);
//        try {
//            List<empWithDept> empWithDepts = deptDao.findAllDeptNamesAndManagers();
//            for (empWithDept empWithDept : empWithDepts) {
//                System.out.println(empWithDept);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

//      @Test
//    public void testFindDeptWithMaxEmployeeCount() {
//        IDeptDao deptDao = session.getMapper(IDeptDao.class);
//        try {
//            empWithDept empWithDept = deptDao.findDeptWithMaxEmployeeCount();
//            System.out.println(empWithDept);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }


    @Test
    public void testAddDepartmentWithManager() {
        IDeptDao deptDao = session.getMapper(IDeptDao.class);
        try {
            Dept dept = new Dept();
            dept.setDeptno("70");
            dept.setDname("测试部");
            dept.setLoc("上海");
            dept.setManagerId(7788);

            // 添加部门
            int insertResult = deptDao.insert(dept);

            // 设置经理
            int updateResult = 0;
            if (dept.getManagerId() != null) {
                updateResult = deptDao.updateEmployeeToManager(
                        dept.getManagerId(),
                        dept.getDeptno()
                );
            }

            session.commit();
            boolean result = (insertResult > 0) &&
                    (dept.getManagerId() == null || updateResult > 0);

            if (result) {
                System.out.println("添加部门成功");
            }
        } catch (Exception e) {
            session.rollback();
            e.printStackTrace();
        }
    }

//    @Test
//    public void testInsert() {
//        IDeptDao deptDao = session.getMapper(IDeptDao.class);
//        try {
//            int row = deptDao.insert(new Dept("50","测试部","测试部"));
//            session.commit();
//            System.out.println("插入了" + row + "行数据");
//            assertEquals(row,1);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

//    @Test
//    public void testSelect() {
//        EmpMapper empMapper = session.getMapper(EmpMapper.class);
//        Emp emp = empMapper.selectByPrimaryKey(7839);
//        System.out.println(emp);
//    }

//    @Test
//    public void testDelete() {
//        IDeptDao deptDao = session.getMapper(IDeptDao.class);
//        try {
//            int row = deptDao.delete(90);
//            session.commit();
//            System.out.println("删除了" + row + "行数据");
//            assertEquals(row,1);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        finally {
//            session.close();
//        }
//    }
}
